package com.deyuanyun.pic.eqpt.service.impl;

import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.eqpt.domain.EquipmentInfo;
import com.deyuanyun.pic.eqpt.mapper.EquipmentInfoMapper;
import com.deyuanyun.pic.eqpt.service.EquipmentInfoService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by Bill.li on 2015/12/30.
 */
@Service
public class EquipmentInfoServiceImpl implements EquipmentInfoService {
    @Resource
    private EquipmentInfoMapper equipmentInfoMapper;


    @Override
    public List<EquipmentInfo> getQuipment(HashMap<String, Object> info) {
    	List<EquipmentInfo> list=equipmentInfoMapper.getQuipment(info);
    	for(EquipmentInfo x:list){
    		if(x.getState()!=null)
    		x.setStateStr(CodeUtil.getName(x.getState()));
    	}
        return list;
    }

    @Override
    public Boolean addQuipment(HashMap<String,Object> param) throws Exception {
        List<String> error=new ArrayList<String>();
        String id= UUID.randomUUID().toString().replaceAll("-", "");
        EquipmentInfo info= (EquipmentInfo) MapUtil.mapToObject(param, EquipmentInfo.class);
        info.setId(id);
        
        if(StringUtils.isEmpty(info.getCustomNumber())){
            error.add("设备编号不能为空");
            param.put("errList", "设备编号不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getModelNumber())){
            error.add("设备型号不能为空");
            param.put("errList", "设备型号不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getUniqueCide())){
            error.add("设备唯一识别码不能为空");
            param.put("errList", "设备唯一识别码不能为空");
            return false;
        }
        param.remove(id);//判断id是否存在
        
        Integer count=equipmentInfoMapper.queryQuipmentCount(MapUtil.beanToMap(info));
        if(count >0)
        {
            error.add("系统已经存在相同的设备!");
            param.put("errList", "系统已经存在相同的设备");
            return false;
        }

        
        equipmentInfoMapper.addQuipment(info);
        return true;//添加设备信息
    }

    @Override
    public Boolean updateQuipment(HashMap<String,Object> param) throws Exception {
        List<String> error=new ArrayList<String>();
        EquipmentInfo info= (EquipmentInfo) MapUtil.mapToObject(param, EquipmentInfo.class);
        if(StringUtils.isEmpty(info.getCustomNumber())){
            param.put("errList", "设备编号不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getModelNumber())){
            error.add("设备型号不能为空");
            param.put("errList", "设备型号不能为空");
            return false;
        }
        if(StringUtils.isEmpty(info.getUniqueCide())){
            error.add("设备唯一识别码不能为空");
            param.put("errList", "设备唯一识别码不能为空");
            return false;
        }
//        param.remove(id);//判断id是否存在
        Integer count=equipmentInfoMapper.queryQuipmentCount(MapUtil.beanToMap(info));
        if(count >0)
        {
            error.add("系统已经存在相同的设备!");
            param.put("errList", "系统已经存在相同的设备");
            return false;
        }

//        info.setId("id");
//        EquipmentInfo record=(EquipmentInfo) MapUtil.mapToObject(param, EquipmentInfo.class);
//        record.setCode(info.getCode());
//        record.setBuyDate(info.getCreateDate());
//        record.setCustomNumber(info.getCustomNumber());
//        record.setCreateUser(info.getCreateUser());
//        record.setId(info.getId());
        equipmentInfoMapper.updateByPrimaryKey(info);
        return true;//更新设备信息
    }

    @Override
    public boolean delQuipment(HashMap param) {
       

        return  equipmentInfoMapper.delQuipment(param);
    }

    @Override
    public List<String[]>  exportQuipment(HashMap param) {
    	List<LinkedHashMap<String, Object>> data =equipmentInfoMapper.exportQuipment(param);
    	for(LinkedHashMap<String, Object> x:data){
    		if(x.get("STATE_")!=null)
    		x.put("STATE_", CodeUtil.getName((String)x.get("STATE_")));
    	}
		//List<List<String[]>> result=new ArrayList<List<String[]>>();
		List<String[]> lsr = JXL.toList(data, "yyyy-MM-dd HH:mm:ss");
		//result.add(lsr);
		
        return lsr;
    }
}
